package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import app.MyApplication;
import greendao.UserDao;
import greendao.entity.User;

/**
 * Created by zx on 2017/3/24.
 */

public class EditorGuardianView extends RelativeLayout {

    AutoCompleteTextView jhrxm;
    EditText jhrdh;
    Spinner jgfs;
    TextView shanchu;

    ArrayAdapter<String> mArrayAdapter;

    UserDao mUserDao;

    public EditorGuardianView(Context context,ArrayList<String> personNameList) {
        super(context);
        setView();
        InitView();
        InItData();
        InitListen();
        mArrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, personNameList);
        jhrxm.setAdapter(mArrayAdapter);
    }





    public void setView() {
        this.addView(LayoutInflater.from(getContext()).inflate(R.layout.bjjhr, null));
    }

    public void InitView() {
        jhrxm = (AutoCompleteTextView) findViewById(R.id.jhrxm);
        jhrdh = (EditText) findViewById(R.id.jhrdh);
        jgfs = (Spinner) findViewById(R.id.jgfs);
        shanchu = (TextView) findViewById(R.id.shanchu);
        ViewHolder mViewHolder=new ViewHolder();
        mViewHolder.jhrxm=jhrxm;
        mViewHolder.jhrdh=jhrdh;
        mViewHolder.jgfs=jgfs;
        mViewHolder.shanchu=shanchu;
       this.setTag(mViewHolder);

        //设置margintop 界面美观
        LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.topMargin=5;
        setLayoutParams(lp);
    }

    public void InItData() {
        mUserDao= MyApplication.getDaoSession().getUserDao();
    }

    public void InitListen() {
        jhrxm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

             List<User> mList =mUserDao.queryBuilder()
                     .where(UserDao.Properties.LjyyxWorkerName.eq(jhrxm.getText().toString()))
                     .list();
                if(mList.size()>0)
                 jhrdh.setText(mList.get(0).getLjyyxWorkerTel());
            }
        });
        shanchu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewGroup)EditorGuardianView.this.getParent()).removeView(EditorGuardianView.this);
            }
        });
    }





    public void setJhrxm(String s) {
        jhrxm.setText(s);
    }

    public void setJhrdh(String s) {
        jhrdh.setText(s);
    }

    public void setJgfs(String s) {
        if (s.equals("现场"))
            jgfs.setSelection(1);
        if (s.equals("添乘"))
            jgfs.setSelection(2);
        if (s.equals("其他"))
            jgfs.setSelection(3);
    }
    public   class ViewHolder{
      public   AutoCompleteTextView jhrxm;
      public   EditText jhrdh;
      public   Spinner jgfs;
      public   TextView shanchu;
    }
}
